import { cloudbaseApp, development } from '@/cloudbaseapi/cloudbase-api'
const db = cloudbaseApp().database()
const $ = db.command.aggregate
const _ = db.command
const { VSDBNAME } = development()

/**
 * 获取诗集列表
 * @param limit
 * @param page
 * @param filterStr
 * @returns {Promise<GetRes>}
 */
export async function getBbkzItem(limit, page) {
  const skip = (page - 1) * limit
  const count = await db.collection(VSDBNAME).count()
  const total = count.total
  const items = await db.collection(VSDBNAME).limit(limit).skip(skip).orderBy('update', 'desc').get()
  items['total'] = total
  return items
}


/**
 * 通过id值查询诗集内容
 */
export function getBbkzItemId(id) {
  return db.collection(VSDBNAME).doc(id).get()
}
/**
 * 控制诗集上下架
 */
export async function updateIsUpper(id, bool) {
  return db.collection(VSDBNAME).where({ _id: id }).update({ isupper: bool})
}
/**
 * 更新或增加版本公司
 */
export function updateBbkzItem(data) {
  const id = data._id
  const newData = { ...data }
  delete newData['_id']
  delete newData['_openid']
  return db.collection(VSDBNAME).doc(id).set(newData)
}
